<template>
  <!-- 仓库表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="700"
    draggable
    @close="closeDialog"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form ref="formRef" :model="formData" label-width="90">
      <el-row>
        <el-col :span="12">
          <el-form-item label="仓库编码" prop="warehouseCode">
            <el-input
              v-model="formData.warehouseCode"
              placeholder="请输入仓库编码"
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="仓库名称" prop="warehouseName">
            <el-input
              v-model="formData.warehouseName"
              placeholder="请输入仓库名称"
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="外来编号" prop="sourceCode">
            <el-input
              v-model="formData.sourceCode"
              placeholder="请输入外来编号"
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="入库冻结" prop="lockInbound">
            <el-select
              v-model="formData.lockInbound"
              placeholder="请选择是否入库冻结"
              disabled
              style="width: 240px"
            >
              <el-option
                v-for="item in dictionaryStore.filterDictList('YesNo')"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出库冻结" prop="lockOutbound">
            <el-select
              v-model="formData.lockOutbound"
              placeholder="请选择是否出库冻结"
              disabled
              style="width: 240px"
            >
              <el-option
                v-for="item in dictionaryStore.filterDictList('YesNo')"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remarks">
            <el-input
              v-model="formData.remarks"
              type="textarea"
              placeholder="请输入备注"
              disabled
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="closeDialog">关 闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary";
import { useDictionary } from "@/utils/useTable"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "WMSWMSWAREHOUSESIMPLIFYSHOW",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  warehouseCode: [
    { required: true, message: "请输入仓库编号", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  warehouseName: [
    { required: true, message: "请输入仓库名称", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  sourceCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  plantId: [{ required: true, message: "请选择供应商", trigger: "change" }],
  lockInbound: [],
  lockOutbound: [],
  warehouseAddress: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  warehouseType: [
    { required: true, message: "请选择仓库类型", trigger: "change" },
  ],
  isVirtual: [],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  warehouseCode: "",
  warehouseName: "",
  sourceCode: "",
  organizationId: "",
  organizationCode: "",
  organizationName: "",
  plantId: "",
  plantCode: "",
  plantName: "",
  lockInbound: "0",
  lockOutbound: "0",
  warehouseAddress: "",
  warehouseType: "1",
  isVirtual: "0",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        warehouseCode,
        warehouseName,
        sourceCode,
        organizationId,
        organizationCode,
        organizationName,
        plantId,
        plantCode,
        plantName,
        lockInbound,
        lockOutbound,
        warehouseAddress,
        warehouseType,
        isVirtual,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        warehouseCode,
        warehouseName,
        sourceCode,
        organizationId,
        organizationCode,
        organizationName,
        plantId,
        plantCode,
        plantName,
        lockInbound,
        lockOutbound,
        warehouseAddress,
        warehouseType,
        isVirtual,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
      });
    }
    formData.lockInbound = String(formData.lockInbound);
    formData.lockOutbound = String(formData.lockOutbound);
    formData.warehouseType = String(formData.warehouseType);
    formData.isVirtual = String(formData.isVirtual);
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.warehouseCode = "";
  formData.warehouseName = "";
  formData.sourceCode = "";
  formData.organizationId = "";
  formData.organizationCode = "";
  formData.organizationName = "";
  formData.plantId = "";
  formData.plantCode = "";
  formData.plantName = "";
  formData.lockInbound = "0";
  formData.lockOutbound = "0";
  formData.warehouseAddress = "";
  formData.warehouseType = "1";
  formData.isVirtual = "0";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 仓库保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/wms/wmswarehousesimplify/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
onMounted(() => {
  getPlantList();
});
const plantList: any = ref([]);
const getPlantList = async () => {
  const res = await request({
    url: "system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 3 },
  });
  plantList.value = res.data;
};

function changePlant(id: any) {
  if (id) {
    formData.plantId = id;
    plantList.value.forEach((item: any) => {
      if (item.id === id) {
        formData.plantCode = item.organizationCode;
        formData.plantName = item.organizationName;
      }
    });
  } else {
    formData.plantId = "";
    formData.plantCode = "";
    formData.plantName = "";
  }
}
</script>
